import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/entity/company_entity.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/widgets/highlight_rich_text.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:nyh_motorcade_app/widgets/search_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/text_button_with_icon.dart';
import 'search_account_manage_controller.dart';

class SearchAccountManagePage extends StatelessWidget {
  final controller = Get.put(SearchAccountManageController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SearchAccountManageController>(
      builder: (controller) {
        return Scaffold(
          backgroundColor: Colours.color_background,
          resizeToAvoidBottomInset: false,
          appBar: SearchAppBar(
            controller: controller.searchController,
            context: context,
            hintText: "请输入账户名称/账户编号",
            onChanged: (value) {
              controller.searchAccountManage(value);
            },
          ),
          body: Container(
            child: Stack(
              fit: StackFit.expand,
              alignment: Alignment.center,
              children: [buildHistoryRecordWidget(), buildSearchResultWidget()],
            ),
          ),
        );
      },
    );
  }

  Widget buildHistoryRecordWidget() {
    return Visibility(
      visible: controller.keyword.isEmpty,
      child: Positioned(
        left: 0,
        top: 0,
        right: 0,
        bottom: 0,
        child: Column(
          children: [
            Container(
              height: 40.w,
              padding: EdgeInsets.only(left: 16.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    "历史搜索",
                    style: TextStyle(
                      color: Colours.color_text_theme,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Visibility(
                    visible: controller.historyRecordList.isNotEmpty,
                    child: IconButton(
                      onPressed: () {
                        controller.deleteHistoryRecord();
                      },
                      icon: Image.asset("images/ic_delete.png"),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: ListView.separated(
                shrinkWrap: true,
                physics: BouncingScrollPhysics(),
                itemBuilder: (context, index) {
                  return buildHistoryRecordItemWidget(index);
                },
                separatorBuilder: (context, index) {
                  return Divider(color: Colours.color_background, height: 1.w, thickness: 1.w);
                },
                itemCount: controller.historyRecordList.length,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget buildHistoryRecordItemWidget(int index) {
    String string = controller.historyRecordList[index];
    return Material(
      child: InkWell(
        onTap: () {
          controller.searchController.text = string;
          controller.searchAccountManage(string);
        },
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 16.w),
          child: Text(
            string,
            style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
    );
  }

  Widget buildSearchResultWidget() {
    return Visibility(
      visible: controller.keyword.isNotEmpty,
      child: Positioned(
        left: 0,
        top: 0,
        right: 0,
        bottom: 0,
        child: MultiStateWidget(
          child: Column(
            children: [
              Container(
                height: 40.w,
                padding: EdgeInsets.only(left: 16.w),
                child: Row(
                  children: [
                    Text(
                      "相关账户(${controller.companySearchList.length})",
                      style: TextStyle(
                        color: Colours.color_text_theme,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: ListView.builder(
                  shrinkWrap: true,
                  physics: BouncingScrollPhysics(),
                  itemCount: controller.companySearchList.length,
                  itemBuilder: (context, index) {
                    return buildSearchResultItemWidget(index);
                  },
                ),
              ),
            ],
          ),
          state: controller.multiState,
        ),
      ),
    );
  }

  Widget buildSearchResultItemWidget(int index) {
    CompanyEntity companyEntity = controller.companySearchList[index];
    bool isNotSettlement = companyEntity.isNotSettlement ?? false;
    return Container(
      margin: EdgeInsets.only(left: 16.w, top: index == 0 ? 12.w : 0, right: 16.w, bottom: 12.w),
      decoration: BoxDecoration(
        color: Colours.color_white,
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10.w),
        child: Material(
          child: InkWell(
            onTap: () {},
            child: Padding(
              padding: EdgeInsets.only(left: 16.w, top: 13.w, right: 16.w, bottom: 8.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  HighlightRichText(
                    text: companyEntity.name ?? "",
                    highlightList: [controller.keyword],
                    highlightStyle: TextStyle(
                      color: Colours.color_theme,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                    ),
                    normalStyle: TextStyle(
                      color: Colours.color_text_theme,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(
                    height: 3.w,
                  ),
                  HighlightRichText(
                    text: companyEntity.code ?? "",
                    highlightList: [controller.keyword],
                    highlightStyle: TextStyle(
                      color: Colours.color_theme,
                      fontSize: 12.sp,
                    ),
                    normalStyle: TextStyle(
                      color: Colours.color_b1b2bb,
                      fontSize: 12.sp,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(
                    height: 9.w,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      TextButtonWithIcon(
                        onPressed: () {
                          controller.setSettlementState(companyEntity.id ?? "", isNotSettlement);
                        },
                        icon: Image.asset("images/ic_${isNotSettlement ? "selected" : "unselect"}.png"),
                        label: Text(
                          "不再结算",
                          style: TextStyle(color: Colours.color_text_theme, fontSize: 12.sp),
                        ),
                        style: ButtonStyle(
                          tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                          minimumSize: MaterialStateProperty.all(Size(0, 0)),
                          padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                        ),
                        textIconPadding: 5.w,
                      ),
                      SizedBox(
                        width: 10.w,
                      ),
                      Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Visibility(
                            visible: !isNotSettlement,
                            child: TextButtonWithIcon(
                              onPressed: () {
                                controller.backResult(companyEntity.id ?? "", companyEntity.name ?? "", companyEntity.code ?? "");
                              },
                              icon: Image.asset("images/ic_bar_chart.png"),
                              label: Text(
                                "公司结算",
                                style: TextStyle(
                                  color: Colours.color_6f7082,
                                  fontSize: 14.sp,
                                ),
                              ),
                              style: ButtonStyle(
                                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                                backgroundColor: MaterialStateProperty.all(Colours.color_white),
                                shape: MaterialStateProperty.all(
                                  RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(6.w),
                                    side: BorderSide(width: 1.w, color: Colours.color_e2e2d5),
                                  ),
                                ),
                                minimumSize: MaterialStateProperty.all(Size(99.w, 30.w)),
                              ),
                              textIconPadding: 5.w,
                            ),
                          ),
                          SizedBox(
                            width: 8.w,
                          ),
                          TextButtonWithIcon(
                            onPressed: () {
                              Get.toNamed(Routes.cashFlow, arguments: companyEntity.id ?? "");
                            },
                            icon: Image.asset("images/ic_money.png"),
                            label: Text(
                              "资金流水",
                              style: TextStyle(
                                color: Colours.color_6f7082,
                                fontSize: 14.sp,
                              ),
                            ),
                            style: ButtonStyle(
                              tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              backgroundColor: MaterialStateProperty.all(Colours.color_white),
                              shape: MaterialStateProperty.all(
                                RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(6.w),
                                  side: BorderSide(width: 1.w, color: Colours.color_e2e2d5),
                                ),
                              ),
                              minimumSize: MaterialStateProperty.all(Size(99.w, 30.w)),
                            ),
                            textIconPadding: 5.w,
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
